﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Server;
using MySql.Data.MySqlClient;

namespace Model.Nova
{
    public class MachineDbHelper
    {
        private const string Conn = "Database='RemoteDB';Data Source='localhost';User Id='root';Password='123456';charset='utf8';pooling=true";
        private const string StrMachindeTable = "machineTb";
        private const string FieldName = "name";
        private const string FieldPasswordHash = "password";
        private const string FieldIdentity = "identity";
        private readonly string _comSelFomate = $"SELECT * from {StrMachindeTable} WHERE {FieldName}=" + "'{0}'";
        private readonly string _comReplaceFomate = $"REPLACE INTO {StrMachindeTable}({FieldIdentity},{FieldName},{FieldPasswordHash})" + " VALUES('{0}', '{1}', '{2}')";

        private MachineDbHelper()
        { }

        private static readonly object SyncRoot = new Object();
        private static volatile MachineDbHelper _instance;
        public static MachineDbHelper Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (SyncRoot)
                    {
                        if (_instance == null)
                            _instance = new MachineDbHelper();
                    }
                }

                return _instance;
            }
        }

        public static MachineDbHelper GetInstance()
        {
            if (_instance == null)
            {
                _instance = new MachineDbHelper();
            }
            return _instance;
        }

        public Machine GetDbMachine(string name)
        {
            try
            {
                string command = string.Format(_comSelFomate, name);
                var dataRow = MySqlHelper.ExecuteDataRow(Conn, command, null);
                //string name = (string)dataRow[FieldName];
                string passwordHash = (string)dataRow[FieldPasswordHash];
                string identity = (string)dataRow[FieldIdentity];

                var dbMachine = new Machine()
                {
                    Identity = identity,
                    NovaId = name,
                    PasswordHash = passwordHash,
                };

                return dbMachine;
            }
            catch (Exception)
            {

                return null;
            }
        }

        public int SaveDbMachine(string strName, string strPasswordHash, string strIdentity)
        {
            int ret;
            try
            {
                string command = string.Format(_comReplaceFomate, strIdentity, strName, strPasswordHash);
                ret = MySqlHelper.ExecuteNonQuery(Conn, command, null);
            }
            catch (Exception)
            {

                return -1;
            }
            return ret;
        }

        public bool NameExist(string strName, string strIdentity)
        {
            string command = string.Format(_comSelFomate, strName);
            try
            {
                var dataRow = MySqlHelper.ExecuteDataRow(Conn, command, null);
                if (dataRow != null)
                {
                    // 用户名一样，但机器不一样，我们可以判断这个用户名被其它机器使用了
                    return ((string)dataRow[FieldIdentity]) != strIdentity;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }
    }
}
